package cn.iocoder.yudao.module.product.service.resourceshare;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.product.controller.admin.resourceshare.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.resourceshare.ResourceShareDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.product.dal.mysql.resourceshare.ResourceShareMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 产品资源分享 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResourceShareServiceImpl implements ResourceShareService {

    @Resource
    private ResourceShareMapper resourceShareMapper;

    @Override
    public Long createResourceShare(ResourceShareSaveReqVO createReqVO) {
        // 插入
        ResourceShareDO resourceShare = BeanUtils.toBean(createReqVO, ResourceShareDO.class);
        resourceShareMapper.insert(resourceShare);
        // 返回
        return resourceShare.getId();
    }

    @Override
    public void updateResourceShare(ResourceShareSaveReqVO updateReqVO) {
        // 校验存在
        validateResourceShareExists(updateReqVO.getId());
        // 更新
        ResourceShareDO updateObj = BeanUtils.toBean(updateReqVO, ResourceShareDO.class);
        resourceShareMapper.updateById(updateObj);
    }

    @Override
    public void deleteResourceShare(Long id) {
        // 校验存在
        validateResourceShareExists(id);
        // 删除
        resourceShareMapper.deleteById(id);
    }

    private void validateResourceShareExists(Long id) {
        if (resourceShareMapper.selectById(id) == null) {
            throw exception(RESOURCE_SHARE_NOT_EXISTS);
        }
    }

    @Override
    public ResourceShareDO getResourceShare(Long id) {
        return resourceShareMapper.selectById(id);
    }

    @Override
    public ResourceShareDO getResourceShareByResourceId(Integer resourceId) {
        return resourceShareMapper.selectByResourceId(resourceId);
    }




    @Override
    public PageResult<ResourceShareDO> getResourceSharePage(ResourceSharePageReqVO pageReqVO) {
        return resourceShareMapper.selectPage(pageReqVO);
    }

}